//
//  YBStarCommentView.m
//  ZhongWangYuanApp
//
//  Created by apple on 18/6/4.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YBStarCommentView.h"

@interface YBStarCommentView ()
{
    NSInteger _starNumber;
}
@property (nonatomic , strong) NSMutableArray * starBtnArray;

@property (nonatomic , strong) UILabel * scoreLabel;
@end

@implementation YBStarCommentView
//自己的大小   [UIScreen zj_screenWith] - ItemWidth * 80    ,  ItemHeight * 98
- (instancetype)initWithFrame:(CGRect)frame starNumber:(NSInteger)starNmuber title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        _starNumber = starNmuber;
        self.currentStarNumber = 0;
        self.starBtnArray = [NSMutableArray array];
        UILabel * titleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 40, ItemHeight * 19, ItemWidth * 165 , ItemHeight * 60) font:15 textColor:[UIColor textGrayColor] text:title];
        [self addSubview:titleLabel];
        
        for (int i = 0; i < starNmuber; i ++) {
            UIButton * starButton = [ToolClass createButtonWithFrame:CGRectMake(ItemWidth * 205 + ItemWidth * 66 * i, ItemHeight * 29, ItemHeight * 40, ItemHeight * 40) text:@"" textColor:[UIColor whiteColor] imageName:@"star_n" font:15];
            [starButton addTarget:self action:@selector(option_starButton:) forControlEvents:UIControlEventTouchUpInside];
            starButton.tag = 1000 + i;
            [self addSubview:starButton];
            [self.starBtnArray addObject:starButton];
        }
        self.scoreLabel = [ToolClass creatLabelWithFrame:CGRectMake([UIScreen zj_screenWith] - ItemWidth * 224, ItemHeight * 19, ItemWidth * 144 , ItemHeight * 60) font:15 textColor:[UIColor textGrayColor] text:@"0分"];
        [self addSubview:self.scoreLabel];
        
    }
    return self;
}

-(void)option_starButton:(UIButton *)sender{
    NSInteger starIndex = sender.tag - 1000;
    for (int i = 0; i < _starNumber; i ++) {
        if (i <= starIndex) {
            UIButton * button = self.starBtnArray[i];
            [button setImage:[UIImage imageNamed:@"sta_s"] forState:UIControlStateNormal];
        }
        else{
            UIButton * button = self.starBtnArray[i];
            [button setImage:[UIImage imageNamed:@"star_n"] forState:UIControlStateNormal];
        }
    }
    self.currentStarNumber = starIndex + 1;
    self.scoreLabel.text = [NSString stringWithFormat:@"%ld分" , self.currentStarNumber * 2];
    if (self.delegate && [self.delegate respondsToSelector:@selector(StarCommentView:didSelectedWithCurrentNumber:)]) {
        [self.delegate StarCommentView:self didSelectedWithCurrentNumber:self.currentStarNumber];
    }
}


-(void)setStarButtonWithStarNumber:(NSInteger)starNumber{
    NSInteger starIndex = starNumber - 1;
    for (int i = 0; i < _starNumber; i ++) {
        if (i <= starIndex) {
            UIButton * button = self.starBtnArray[i];
            [button setImage:[UIImage imageNamed:@"sta_s"] forState:UIControlStateNormal];
        }
        else{
            UIButton * button = self.starBtnArray[i];
            [button setImage:[UIImage imageNamed:@"star_n"] forState:UIControlStateNormal];
        }
    }
    self.currentStarNumber = starIndex + 1;
    self.scoreLabel.text = [NSString stringWithFormat:@"%ld分" , self.currentStarNumber * 2];
}


@end
